using System.Data;
using System.Data.SqlClient;
using Provausio.Common.Data;
using Provausio.Common.Objects;
using Provausio.Common.Utility.Extensions;

namespace TrainingManagement.api.Models.Data.CRUD.DepartmentTree
{
    public class DepartmentTreeInsertDataSource : NonQuerySource<BusinessObjects.DepartmentTree>
    {
        public override bool Execute(BusinessObjects.DepartmentTree request)
        {
            const string insertDeptTree = "dbo.training_api_DepartmentTreeInsert";
            var parameters = new SqlParameter[4];
            parameters[0] = new SqlParameter("@LocationID", SqlDbType.Int){Value = request.LocationId};
            parameters[1] = new SqlParameter("@Parent", SqlDbType.NVarChar) {Value = request.ParentId};
            parameters[2] = new SqlParameter("@Child", SqlDbType.NVarChar) { Value = request.ChildId };
            parameters[3] = new SqlParameter("@DepartmentTreeID", SqlDbType.Int){Direction = ParameterDirection.Output};

            var result = SqlHelper.ExecuteNonQuery(
                    ConnectionStrings.TrainingDb,
                    CommandType.StoredProcedure,
                    insertDeptTree,
                    parameters) != 0;
                request.DepartmentTreeId = parameters[3].Value.DbTo<int>();
                return result;
        }
    }
}